package myatm;

public class ATM {

    private double money;
    private Card validatedCard;

    public class NotEnoughMoneyInAccount extends RuntimeException {}
    public class NotEnoughMoneyInATM extends RuntimeException {}
    public class NoCardInserted extends RuntimeException {}
    public class NegativeAmountTaken extends RuntimeException {}
    public class NegativeMoneyInATM extends RuntimeException {}


    ATM(double moneyInATM){

        if(moneyInATM < 0)
            throw new NegativeMoneyInATM();

        money = moneyInATM;
    }

    public double getMoneyInATM() {
        return money;
    }

    public boolean validateCard(Card card, int pinCode){

        validatedCard = null;

        if (!card.checkPin(pinCode)){
            return false;
        }

        if (card.isBlocked()){
            return false;
        }

        validatedCard = card;

        return true;
    }

    public double checkBalance(){

        if(validatedCard == null)
            throw new NoCardInserted();
        else
            return validatedCard.getAccount().getBalance();
    }

    public double getCash(double amount){

        if(amount < 0)
            throw new NegativeAmountTaken();

        if(validatedCard == null)
            throw new NoCardInserted();

        if(amount > validatedCard.getAccount().getBalance())
            throw new NotEnoughMoneyInAccount();

        if(amount > money)
            throw new NotEnoughMoneyInATM();

        validatedCard.getAccount().withdrow(amount);

        money -= amount;

        return validatedCard.getAccount().getBalance();
    }
}
